package com.jshongli.myapplication.inspect;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.jshongli.myapplication.R;
import com.jshongli.myapplication.base.BaseActivity;
import com.jshongli.myapplication.common.utils.StringUtils;
import com.jshongli.myapplication.common.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class TrackActivity extends BaseActivity {

    @BindView(R.id.mapView)
    MapView mMapView;

    List<LatLng> points = new ArrayList<LatLng>();
    BaiduMap mBaiduMap;

    String id = "";
    String track = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_track;
    }

    @Override
    protected void initView() {
        super.initView();

        mToolbar.setTitle("巡查轨迹");
        initToolbarBack(mToolbar);
    }

    //region 轨迹
    double lanSum = 0;
    double lonSum = 0;

    @Override
    protected void initData() {
        super.initData();

        mBaiduMap = mMapView.getMap();

        mMapView.showScaleControl(false);
        mMapView.showZoomControls(false);

        track = getIntent().getStringExtra("track");
        lanSum = 0;
        lonSum = 0;

        if(track != null)
        {
            if(track.length()>10){
                String[] trackarr = track.substring(2,track.length()-2).split("],\\[");
                for(String gj : trackarr)
                {
                    String[] jwdarr = gj.split(",");

                    LatLng latLng = new LatLng(Double.parseDouble(jwdarr[1]), Double.parseDouble(jwdarr[0]));
                    points.add(latLng);

                    lanSum += StringUtils.stringToDouble(jwdarr[1]);
                    lonSum += StringUtils.stringToDouble(jwdarr[0]);
                }

                setTrack();
            }

        }else {
            ToastUtils.show(mContext, "轨迹暂无");
        }


    }

    //region 生命周期
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }
    //endregion

    //region 设置起终点、轨迹
    private void setTrack() {



        if (points.size() > 1) {
            //绘制轨迹线
            OverlayOptions mOverlayOptions = new PolylineOptions()
                    .width(10)
                    .color(0xAAFF0000)
                    .points(points);
            Overlay mPolyline = mBaiduMap.addOverlay(mOverlayOptions);

            LatLng endPoint = points.get(points.size() - 1);

            //end图标
            BitmapDescriptor bitmap = BitmapDescriptorFactory
                    .fromResource(R.mipmap.map_end);
            //构建MarkerOption，用于在地图上添加Marker
            OverlayOptions option = new MarkerOptions()
                    .position(endPoint)
                    .icon(bitmap);
            //在地图上添加Marker，并显示
            mBaiduMap.addOverlay(option);
        }

        if (points.size() > 0) {
            LatLng startPoint = points.get(0);
            //构建Marker图标
            BitmapDescriptor bitmap = BitmapDescriptorFactory
                    .fromResource(R.mipmap.map_start);
            //构建MarkerOption，用于在地图上添加Marker
            OverlayOptions option = new MarkerOptions()
                    .position(startPoint)
                    .icon(bitmap);
            //在地图上添加Marker，并显示
            mBaiduMap.addOverlay(option);

            //计算中心点位
//            LatLng target = new LatLng(lanSum / points.size(), lonSum / points.size());
//            MapStatus.Builder builder = new MapStatus.Builder();
//            builder.target(target).zoom(18f);
//            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));


        }

        //调整缩放级别：将所有marker显示在屏幕内
        mBaiduMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
                //设置多有点在一个屏幕里面
                LatLngBounds.Builder builder = new LatLngBounds.Builder();
                for (LatLng latLng : points) {
                    builder.include(latLng);
                }
                MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLngBounds(builder.build());
                mBaiduMap.setMapStatus(mapStatusUpdate);
                MapStatusUpdate msu = MapStatusUpdateFactory.zoomBy(-0.8f);
                mBaiduMap.setMapStatus(msu);
            }
        });



    }

    //endregion
}